package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.FeedbackDao;
import com.chunfeng.mypen.entity.Feedback;
import com.chunfeng.mypen.properties.SystemProperties;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 反馈业务层实现
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/22
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackDao, Feedback> implements FeedbackService {

    /**
     * 反馈Dao
     */
    @Autowired
    private FeedbackDao feedbackDao;
    /**
     * 系统设置
     */
    @Autowired
    private SystemProperties systemProperties;


    /**
     * 条件查询反馈
     *
     * @param current  当前页
     * @param size     每页长度
     * @param feedback 条件
     * @return JSON
     */
    @Override
    public JsonRequest<List<Feedback>> lookAllFeedback(Integer current, Integer size, Feedback feedback) {
        Page<Feedback> feedbackPage = feedbackDao.selectAllFeedback(new Page<>(current, size), feedback);
        //拿到列表
        List<Feedback> feedbacks = feedbackPage.getRecords();
        if (feedbacks.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        Map<String, Object> page = new HashMap<>();
        //记录总页数
        page.put(systemProperties.getPageKeyName(), feedbackPage.getTotal());
        return JsonRequest.success(feedbacks, page);
    }

    /**
     * 新增一条反馈信息
     *
     * @param feedback 反馈信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> insertOneFeedBack(Feedback feedback) {
        int column = feedbackDao.insert(feedback);
        if (column < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 修改一条反馈信息
     *
     * @param feedback 反馈信息
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> updateOneFeedback(Feedback feedback) {
        Feedback feedback1 = feedbackDao.selectById(feedback.getId());
        if (Objects.isNull(feedback1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = feedbackDao.updateById(feedback1);
        if (column < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除反馈
     *
     * @param ids ID
     * @return JSON
     */
    @Override
    public JsonRequest<Integer> deleteFeedback(List<String> ids) {
        Integer size = feedbackDao.selectCountById(ids);
        if (size != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = feedbackDao.deleteBatchIds(ids);
        if (column < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }
}
